import { BaseObject } from './../../../../types/global.d';
import { Module } from 'vuex';
import type { IDashboardState } from './types';
import type { IRootState } from '@/store/types';

import {
  getCategoryGoodsCount,
  getCategoryGoodsSale,
  getCategoryGoodsFavor,
  getAddressGoodsSale,
} from '@/service/main/analysis/dashboard';

const dashboardModule: Module<IDashboardState, IRootState> = {
  namespaced: true,

  state: {
    categoryGoodsCount: [],
    categoryGoodsSale: [],
    categoryGoodsFavor: [],
    addressGoodsSale: [],
  },

  mutations: {
    setState(state, payload: BaseObject) {
      if (Object.keys(payload).length) {
        for (const key in payload) {
          const value = payload[key];
          if (key in state) {
            state[key] = value;
          }
        }
      }
    },
  },

  actions: {
    async getDashboardAction({ commit }) {
      const cData = {
        categoryGoodsCount: [],
        categoryGoodsSale: [],
        categoryGoodsFavor: [],
        addressGoodsSale: [],
      };

      commit('setState', cData);

      const categoryCountResult = await getCategoryGoodsCount();
      cData.categoryGoodsCount = categoryCountResult.data;

      const categorySaleResult = await getCategoryGoodsSale();
      cData.categoryGoodsSale = categorySaleResult.data;

      const categoryFavorResult = await getCategoryGoodsFavor();
      cData.categoryGoodsFavor = categoryFavorResult.data;

      const addressSaleREsult = await getAddressGoodsSale();
      cData.addressGoodsSale = addressSaleREsult.data;

      commit('setState', cData);
    },
  },
};

export default dashboardModule;
